<?php

/*

Battle 1-player (battle_1player.php)

*/

$page_title = "Battle 1-player";
$rank_check = 1;
include "header.inc.php";

$checkStatus = fetch("SELECT * FROM one_player_battles2 WHERE userid = '$userid' AND game = '$game'");
if ($checkStatus[id])
{
	die(header(error("battling_1p.php?game=$game","You are already in a fight!")));
}

if ($act == "prestart")
{
	$findEnemy = fetch("SELECT * FROM one_players2 WHERE guys_left > '0' AND id = '$id' AND game = '$game'");
	if (!$findEnemy[id])
	{
		die(header(error("battle_1player.php?game=$game","You cannot fight this enemy right now.")));
	}

	$findpetlist = mysql_query("SELECT * FROM user_pets2 WHERE owner = '$userid' AND current_hp > '0' AND hunger > '4' AND game = '$game'");
	$numUsablePets = mysql_num_rows($findpetlist);
	if ($numUsablePets > 0)
	{
		while ($getpetlist = mysql_fetch_array($findpetlist))
		{
			$petList .= "<option value=$getpetlist[id]>$getpetlist[name] (level 	$getpetlist[level])";
		}
		$petList = " <select name=usepet>$petList</select> <input type=submit value=\"Fight!\">";
	}
	else
	{
		$petList = "<br><b>You have no usable pets, please go heal or feed your pets.";
	}
ECHO <<<END

$openHTML



<p align=center>
<img src=images/user_images/opg_$game/enemies/enemy_$findEnemy[id].gif><br>$findEnemy[name] - Level $findEnemy[level]
</p>

<form action=battle_1player.pro.php?game=$game&id=$id method=post>
<p align=center>
Fight with:$petList
</p>

$closeHTML

END;
}

if (!$act)
{

print "$openHTML";

ECHO <<<END

<p align=center>
Start a Table |
<a href=battle_1player.php?game=$game>1-player Battle</a> |
View Open Tables |
View Your Tables |
View Challenges You Made |
<a href=battle_items.php?game=$game>Equipped items</a>
</p>

<p align=center><table>
END;

if (!$min) { $min = 0; }

if (!$min) { $min = 0; }
$max = $min + 12;

$getEnemies = mysql_query("SELECT * FROM one_players2 WHERE guys_left > '0' AND game = '$game' ORDER BY level ASC");

$x = 0;
while ($array = mysql_fetch_array($getEnemies))
{
	$id = $array[id];
	$y = $x % 3;
	if ($y == 0)
	{
		echo "<tr width=100%>";
	}

	echo "
	<td valign=top>
	<p align=center>
	<img src=$base_url/images/user_images/opg_$game/enemies/enemy_$array[id].gif>
	<br><b>$array[name]</b></p>

	<p>
	Level: $array[level]<Br>
	Wins: $array[wins]<Br>
	Loses: $array[loses]<br>
	Left to fight: $array[guys_left]
	</p>

	<p align=center><a href=battle_1player.php?game=$game&id=$array[id]&act=prestart>Challenge</a></p>
	</td>";
	if ($y == 2)
	{
		echo "</tr>";
	}
	$x++;
}

echo "</table></p>";

if ($numQuery > $max)
{
	echo "<p align=center><a href=$base_url/battle_1player.php?game=$game&min=$max><b>Next page</b></a></p>";
}
if ($min != 0)
{
	$prev_min = $min - 12;
	echo "<p align=center><a href=$base_url/battle_1player.php?game=$game&min=$prev_min><b>Previous page</b></a></p>";
}

print "$closeHTML";
}

?>